package com.xh.bussiness.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.exam.bean.dto.PaperPublishDto;
import com.xh.bussiness.exam.bean.request.PaperPublishQueryRequest;
import com.xh.bussiness.exam.bean.request.PaperPublishSaveRequest;
import com.xh.bussiness.exam.dao.PaperPublishMapper;
import com.xh.bussiness.exam.model.PaperPublish;
import com.xh.bussiness.exam.service.IPaperPublishService;
import com.xh.bussiness.tiku.bean.dto.PaperDto;
import com.xh.bussiness.tiku.service.IPaperService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.ObjectUtils;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/4/9 14:49
 **/
@Service
public class PaperPublishServiceImpl extends BaseServiceImpl<PaperPublishMapper,PaperPublish> implements IPaperPublishService {

    @Autowired
    private IPaperService paperService;

    @Override
    public String addPaperPublish(PaperPublishSaveRequest bean) {
        PaperPublish paperPublish=new PaperPublish();
        ObjectUtils.copyNonNullProperties(bean,paperPublish);
        this.dao.insert(paperPublish);
        return null;
    }

    @Override
    public String modifyPaperPublish(PaperPublishSaveRequest bean) {
        if(StringUtils.isEmpty(bean.getId())){
            return "ID不能为空";
        }
        PaperPublish paperPublish=this.dao.selectById(bean.getId());
        if(paperPublish==null){
            return "当前操作数据不存在";
        }
        ObjectUtils.copyNonNullProperties(bean,paperPublish);
        try {
            this.dao.updateById(paperPublish);
        }catch (Exception ex){

        }
        return null;
    }

    @Override
    public String modifyPaperPublishStatus(PaperPublishSaveRequest bean) {
        if(StringUtils.isEmpty(bean.getId())){
            return "ID不能为空";
        }
        if(bean.getPublishStatus()==null){
            return "状态不能为空";
        }
        PaperPublish paperPublish=this.dao.selectById(bean.getId());
        if(paperPublish==null){
            return "当前操作数据不存在";
        }
        paperPublish.setPublishStatus(bean.getPublishStatus());
        try {
            this.dao.updateById(paperPublish);
        }catch (Exception ex){

        }
        return null;
    }

    @Override
    public XhPage queryPaperPublish(XhPage page, PaperPublishQueryRequest bean) {
        QueryWrapper qw=new QueryWrapper();
        XhPage pageResult= (XhPage) this.selectPage(page,qw,PaperPublishDto.class);

        List<PaperPublishDto> list= (List<PaperPublishDto>)pageResult.getRecords();
        for (PaperPublishDto paperPublishDto : list) {
            paperPublishDto.setPaper(paperService.getSimplePaper(paperPublishDto.getPaperId()));
        }
        pageResult.setRecords(list);
        return pageResult;
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(PaperPublishQueryRequest bean, QueryWrapper<PaperPublish> qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(bean.getType()!=null){
            qw.eq("type",bean.getType());
        }
        if(bean.getSubjectId()!=null){
            qw.eq("subject_id",bean.getSubjectId());
        }
        if(bean.getGradeId()!=null){
            qw.eq("grade_id",bean.getGradeId());
        }
        if(bean.getSchoolId()!=null){
            qw.eq("school_id",bean.getSchoolId());
        }
        if(bean.getClassId()!=null){
            qw.eq("class_id",bean.getClassId());
       }
        if(bean.getClassTypeId()!=null){
            qw.eq("class_type_id",bean.getClassTypeId());
       }
        if(bean.getCurrId()!=null){
            qw.eq("curr_id",bean.getCurrId());
        }
        if(bean.getCurrOrderNo()!=null){
            qw.eq("curr_order_no",bean.getCurrOrderNo());
       }
        if(bean.getCourseId()!=null){
            qw.eq("course_id",bean.getCourseId());
        }
        if(bean.getStudentId()!=null){
            qw.eq("student_id",bean.getStudentId());
        }
        if(bean.getTeacherId()!=null){
            qw.eq("teacher_id",bean.getTeacherId());
        }
        if(bean.getPublishStatus()!=null){
            qw.eq("publish_status",bean.getPublishStatus());
        }
        if(bean.getOpenTime()!=null){
            qw.lambda().and(obj -> obj.le(PaperPublish::getOpenTime,bean.getOpenTime()).or().isNull(PaperPublish::getOpenTime));
        }
        if(bean.getCloseTime()!=null){
            qw.lambda().and(obj -> obj.ge(PaperPublish::getCloseTime,bean.getCloseTime()).or().isNull(PaperPublish::getCloseTime));
        }

        return qw;
    }

    @Override
    public PaperPublishDto getPaperPublishById(String id) {
        PaperPublish paperPublish= this.dao.selectById(id);
        PaperPublishDto result=new PaperPublishDto();
        ObjectUtils.copyNonNullProperties(paperPublish,result);
        PaperDto paper=paperService.getPaper(paperPublish.getPaperId());
        result.setPaper(paper);

        return result;
    }
}
